<?php
if(!defined('IN_SONG')) exit('No direct script access allowed');  
/**
 * Class module
 * 
 * @package         Kernel
 * @subpackage      Module
 * @version         0.1
 * @copyright       Song (c) 2011
 */ 
if(!class_exists('Object')) {
    require_once KERNEL.DS.'object.php';
}  
class Controller extends Object
{
    /**
    * Var Model
    * 
    * @var String
    */
    protected $_model;
    
    /**
    * Var Module
    * 
    * @var String
    */
    protected $_controller;  
    
    /**
    * Var Template
    * 
    * @var Object
    */
    protected $Template;
    
    /**
    * Var Input
    * 
    * @var Object
    */
    protected $Input;
    
    /**
    * Var User
    * 
    * @var Object
    */
    protected $user;
    
    /**
    * Var rules
    * 
    * @var array  
    */
    public $rules;
    
    /**
    * Var messages
    * 
    * @var array  
    */
    public $messages;
    
    public $layout = 'default';
    
    /**
    * Start class module
    * 
    * @param String $model
    * @param String $module
    * @return Module
    */
    function __construct() 
    {                 
        /*if ($this->name === null) {
			$r = null;
			if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
				__("Controller::__construct() : Can not get or parse my own class name, exiting.");
				$this->_stop();
			}       
			$this->name = $r[1];
		}*/  
        parent::__construct();     
        //print_r($this); die;
        $this->_model = $model = $this->name;    
        // Object Model
        if(include(APP.DS.'web'.DS.'models'.DS.strtolower($model) . '.php')){
            $this->$model =& new $model;
        }       
        // Object Helper 
        if($this->helper){ 
            for($h=0;$h<sizeof($this->helper);$h++){
                $this->__helper($this->helper[$h]);
            }
        }
        // Object Template
        $this->Template =& new Template(APP.DS.'web'.DS.'views');
        
        // Object Input
        $this->Input =& new Input();
        
           
    }
    
    /**
    * Object helper
    * 
    * @param String $name
    * @param Array $params
    * @return 
    * @access Protected
    */
    protected function __helper($name, $params = false)
    {
        if(include(HELPER .DS. strtolower($name) .'.helper.php')){
            $helperName = $name .'Helper';
            $this->$helperName =& new $helperName($params);  
        }    
    } 
    
    /**
    * Run
    * 
    * @return String
    * @access Public
    */
    public function run($params){     
        // Set layout admin
        if($params['subfix'] === 'admin'){    
            $this->layout = 'admin';
        }      
        // Submit form
        /*if($_POST['formid'] == md5($this->_controller)){ 
            $this->isSubmit();
        }*/      
        // return html
        $this->Template->setFile('Layout', 'layouts'.DS. $this->layout .'.html', true); 
        $this->Template->setVar(array(
            'rootUrl' => URL.DS,
            'title_for_layout' => '', 
            'content_for_layout' => $this->$params['action']()
        ));   
        $this->Template->parse('OUT', 'Layout');
        return $this->Template->p('OUT');    
    }
    
    
    
    /**
    * End class module  
    */
    function __destruct() {
    	
    }
}
?>
